#include "login.h"
#include "ui_login.h"

Login::Login(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Login)
{
    ui->setupUi(this);
    //无边框化
    this->setWindowFlags( Qt::FramelessWindowHint );
    //设置窗口标题
    this->setWindowTitle("登录");
    //设置密文登录
    ui->password->setEchoMode(QLineEdit::Password);
    //键盘初始化
    ui->username->setFocus();
    keyboard->initFocusWidget(ui->widget);
    keyboard->hide();
    isKeyBoardShow = 0;
    //ui美化
    this->initStyleSheet();
    //close keyboard
    connect(keyboard,&frmKeyBoard::hiding,this,&Login::hidekeyboard);
}

Login::~Login()
{
    delete ui;
}

void Login::initStyleSheet()
{
    QString qss;

    qss += "QLineEdit{border-width: 1px; border-radius: 10px; border-style: solid; border-color:rgb(98,183,152); }";
    qss += "#password{border-color:rgb(193,150,144);}";
    /*
    qss += "QPushButton:hover{background-color:rgb(229,229,229); color:black;}";
    qss += "QPushButton:pressed,QPushButton:checked{background-color:rgb(0,118,215);}";
    qss += "#pushButton_closeKeyboard{background-color:rgba(0,0,0,0); border:0px}";
    qss += "#pushButton_closeKeyboard:hover{background-color:#b30220;}";
    qss += "#pushButton_space{min-width:500px;}";
    qss += "#pushButton_backspace,#pushButton_shift{min-width:100px;}";
    qss += "#pushButton_enter{min-width:120px;}";
    qss += "#pushButton_tab,#pushButton_ctrl{min-width:70px;}";
    qss += "#pushButton_capsLock{min-width:80px;}";
    qss += "#pushButton_up{min-width:150px;}";
    */
    qss += "QHeaderView::section {"
                   "background-color: black;"
                   "border: 1px solid gray;"
                   "height: 5px; /* 调整高度 */"
                   "}";
    this->setStyleSheet(qss);
}

void Login::on_login_clicked()
{
    UserManager userManager;

    QString username = ui->username->text();
    QString password = ui->password->text();
    // 输入用户名和密码，登录成功则返回 true，失败则返回 false
    User user = userManager.login(username,password);

    if (!user.username.isEmpty()) {
        //qDebug() << "Login successful for user:" << user.username;
        this->close();
        //将用户信息传给各个界面
        Permission::User = username;
        qDebug()<<Permission::User;
        //如果不是管理员，则会在页面中将一些内容自动隐藏
        if(Permission::User != "admin"){
            emit admin(0);
        }else{
            emit admin(1);
        }
        emit reset();
        keyboard->hide();
        isKeyBoardShow = 0;
        emit display(1); //s

    } else {
        //qDebug() << "Login failed for user:" << username;
        QMessageBox::warning(this,tr("login failed"),tr("Username or Password wrong！"),QMessageBox::Ok);
        ui->username->clear();
        ui->password->clear();
        ui->username->setFocus();
    }
}


void Login::on_keyboard_clicked()
{
    if(isKeyBoardShow == 0){
        keyboard->setGeometry(0, 175, 431, 166);//for one certain location
        keyboard->show();   
        isKeyBoardShow = 1;
    }else{
        keyboard->hide();
        isKeyBoardShow = 0;
    }
}

//hide keyboard by keyboard's button
void Login::hidekeyboard()
{
    keyboard->hide();
    isKeyBoardShow = 0;
}

